/*Copyright (C) 2009 - 2011 MASSART GAUTHIER

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../headers.h"

void snake_affichage_mat(char matrice[1000], char niveau[50], char auteur[100], long vie, long score, long niveau_actuel, long nombre_niveau) {
	long k = 0, y = 5, x = x_begin_center(50);
	CSLP_ColorText(COLOR_WHITE);

	while(k != 1000) {
		move(y, x);

		if(matrice[k] == '#') {
			afficher_char_fullblock(1, COLOR_WHITE);
		}
		else if(matrice[k] == '>') {
			CSLP_ColorText(COLOR_GREEN);
			addstr("▓");
		}
		else if(matrice[k] == '=') {
			CSLP_ColorText(COLOR_GREEN);
			addstr("▒");
		}
		else if(matrice[k] == 'C') {
			CSLP_ColorText(COLOR_RED);
			addstr("♥");
		}
		else if(matrice[k] == 'O') {
			CSLP_ColorText(COLOR_YELLOW);
			addstr("☼");
		}
		else if(matrice[k] == 'o') {
			CSLP_ColorText(COLOR_YELLOW);
			addstr("ø");
		}
		else {
			CSLP_ColorText(COLOR_WHITE);
			addch(matrice[k]);
		}

		x++;
		k++;

		if(x == x_begin_center(50) + 50) {
			x = x_begin_center(50);
			y++;
		}
	}

	CSLP_ColorText(COLOR_WHITE);
	mvaddstr(26, x_begin_center(50) + 25, "Niveau : ");
	CSLP_ColorText(CT);
	printw(" %s        ", niveau);
	CSLP_ColorText(COLOR_WHITE);
	mvaddstr(27, x_begin_center(50) + 25, "Auteur : ");
	CSLP_ColorText(CT);
	printw(" %s        ", auteur);
	CSLP_ColorText(COLOR_WHITE);
	mvaddstr(28, x_begin_center(50), "Niveau actuel :");
	CSLP_ColorText(CT);
	printw(" %ld ", niveau_actuel);

	CSLP_ColorText(COLOR_WHITE);
	mvaddstr(26, x_begin_center(50), "Score :");
	CSLP_ColorText(CT);
	printw(" %ld ", score);
	CSLP_ColorText(COLOR_YELLOW);
	addstr("☺");
	CSLP_ColorText(COLOR_WHITE);
	mvaddstr(27, x_begin_center(50), "Vie   :");
	CSLP_ColorText(CT);
	printw(" %ld ", vie);
	CSLP_ColorText(COLOR_RED);
	addstr("♥");
	CSLP_ColorText(COLOR_WHITE);
	mvaddstr(28, x_begin_center(50) + 25, "Nombre de niveaux :");
	CSLP_ColorText(CT);
	printw(" %ld ", nombre_niveau);
	CSLP_ColorText(CT);

}

/* Fonction affichant l'écran d'édition
*/
void snake_display_editor(SNAKE_CASE_T matrix[SNAKE_MAT_X][SNAKE_MAT_Y]) {
	clear();
	/* Titre */
	afficher_titre(TITRE_SNAKE);
	/* Matrice */
	snake_display_matrix(matrix);
	/* Informations sur le jeu */
	move(14, x_begin_center(50) - 27);
	CSLP_ColorText(COLOR_RED);
	afficher_char_bullet(1);
	CSLP_ColorText(CT);
	addstr("Liste des touches :");
	CSLP_ColorText(COLOR_RED);
	mvprintw(15, x_begin_center(50) - 25, "%c", SNAKE_KEY_QUIT);
	CSLP_ColorText(CT);
	addstr(" : Quitter");
	CSLP_ColorText(COLOR_RED);
	mvprintw(16, x_begin_center(50) - 25, "%c", SNAKE_KEY_EMUR);
	CSLP_ColorText(CT);
	addstr(" : Ajouter un mur");
	CSLP_ColorText(COLOR_RED);
	mvprintw(17, x_begin_center(50) - 25, "%c", SNAKE_KEY_EVIDE);
	CSLP_ColorText(CT);
	addstr(" : Vider la case");
	move(15, x_begin_center(50) + 60);
	CSLP_ColorText(COLOR_RED);
	afficher_touche(touche_haut);
	CSLP_ColorText(CT);
	addstr(" : Se déplacer vers le haut");
	move(16, x_begin_center(50) + 60);
	CSLP_ColorText(COLOR_RED);
	afficher_touche(touche_bas);
	CSLP_ColorText(CT);
	addstr(" : Se déplacer vers le bas");
	move(17, x_begin_center(50) + 60);
	CSLP_ColorText(COLOR_RED);
	afficher_touche(touche_droite);
	CSLP_ColorText(CT);
	addstr(" : Se déplacer vers la droite");
	move(18, x_begin_center(50) + 60);
	CSLP_ColorText(COLOR_RED);
	afficher_touche(touche_gauche);
	CSLP_ColorText(CT);
	addstr(" : Se déplacer vers la gauche");

	refresh();
}

/* Fonction affichant la légende
*/
void snake_afficher_info(void) {
	CSLP_ColorText(CT);
	mvaddstr(5, x_begin_center(50) - 25, "Touche Haut   : ");
	CSLP_ColorText(COLOR_RED);

	afficher_touche(touche_haut);

	CSLP_ColorText(CT);
	mvaddstr(6, x_begin_center(50) - 25, "Touche Bas    : ");
	CSLP_ColorText(COLOR_RED);

	afficher_touche(touche_bas);

	CSLP_ColorText(CT);
	mvaddstr(7, x_begin_center(50) - 25, "Touche Droite : ");
	CSLP_ColorText(COLOR_RED);

	afficher_touche(touche_droite);

	CSLP_ColorText(CT);
	mvaddstr(8, x_begin_center(50) - 25, "Touche Gauche : ");
	CSLP_ColorText(COLOR_RED);

	afficher_touche(touche_gauche);

	CSLP_ColorText(COLOR_YELLOW);
	mvaddstr(5, x_begin_center(50) + 60, "ø");
	CSLP_ColorText(CT);
	addstr(" : 250 points");
	CSLP_ColorText(COLOR_YELLOW);
	mvaddstr(6, x_begin_center(50) + 60, "☼");
	CSLP_ColorText(CT);
	addstr(" : 500 points + accélèration");
	CSLP_ColorText(COLOR_RED);
	mvaddstr(7, x_begin_center(50) + 60, "♥");
	CSLP_ColorText(CT);
	addstr(" : 1000 points + 1 vie");
}

/* Fonction affichant la matrice
*/
void snake_display_matrix(SNAKE_CASE_T matrix[SNAKE_MAT_X][SNAKE_MAT_Y]) {
	int y, x;

	for(y = 0; y < SNAKE_MAT_Y; y++) {
		move(SNAKE_MAT_ORG_Y + y, SNAKE_MAT_ORG_X);

		for(x = 0; x < SNAKE_MAT_X; x++) {
			snake_display_case(matrix[x][y]);
		}
	}
}

/* Fonction permettant d'afficher une case
*/
void snake_display_case(SNAKE_CASE_T cel) {
	if(cel == SNAKE_MUR) {
		CSLP_ColorText(SNAKE_COL_MUR);
		addstr(SNAKE_WCH_MUR);
	}
	else if(cel & SNAKE_IMPLACABLE) {
		CSLP_ColorText(SNAKE_COL_IMPLACABLE);
		addstr(SNAKE_WCH_IMPLACABLE);
	}
	else if(cel == SNAKE_VIDE) {
		addch(' ');
	}
}

/* Fonction rafraichissant la valeur d'une case
*/
void snake_refresh_case(IPoint *pos, SNAKE_CASE_T value) {
	if(pos != NULL) {
		move(pos->y + SNAKE_MAT_ORG_Y, pos->x + SNAKE_MAT_ORG_X);
	}

	snake_display_case(value);
	refresh();
}
